package com.rongji.dfish.hello.index.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.rongji.dfish.base.DfishException;
import com.rongji.dfish.base.Page;
import com.rongji.dfish.base.Utils;
import com.rongji.dfish.framework.FilterParam;
import com.rongji.dfish.hello.entity.PubUser;
import com.rongji.dfish.hello.index.service.UserService;
import com.rongji.dfish.hello.index.view.UserView;
import com.rongji.dfish.hello.pub.controller.CommonController;
import com.rongji.dfish.hello.pub.view.CommonView;
import com.rongji.dfish.ui.command.AlertCommand;
import com.rongji.dfish.ui.command.CommandGroup;
import com.rongji.dfish.ui.command.JSCommand;
import com.rongji.dfish.ui.command.ReplaceCommand;
import com.rongji.dfish.ui.layout.View;

@Controller
@RequestMapping("/user")
public class UserController extends CommonController {
	@Autowired
	private UserService userService;
	@Autowired
	private UserView userView;

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public UserView getUserView() {
		return userView;
	}

	public void setUserView(UserView userView) {
		this.userView = userView;
	}

	@RequestMapping("/index")
	@ResponseBody
	public View index(HttpServletRequest request) throws Exception {
		Page page = getPage(request);
		FilterParam fp = userView.getParam(request);
		List<PubUser> userList = userService.findUsers(page, fp);
		View view = userView.buildIndexView(userList, page, fp);
		return view;
	}

	@RequestMapping("/search")
	@ResponseBody
	public Object search(HttpServletRequest request) throws Exception {
		View view = index(request);
		CommandGroup cg = new CommandGroup();
		cg.add(new ReplaceCommand(view.findNodeById(CommonView.W_MAIN_CONTENT)));
		cg.add(new ReplaceCommand(view.findNodeById(CommonView.W_MAIN_PAGE)));
		return cg;
	}

	@RequestMapping("/edit")
	@ResponseBody
	public Object edit(HttpServletRequest request) throws Exception {
		String userId = request.getParameter("userId");
		PubUser user = userService.get(userId);
		View view = userView.buildEditView(user);
		return view;
	}

	@RequestMapping("/save")
	@ResponseBody
	public Object save(HttpServletRequest request) throws Exception {
		PubUser user = new PubUser();
		bind(request, user);
		if (Utils.isEmpty(user.getUserId())) {
			user.setUserId(userService.getNewId());
			userService.save(user);
		} else {
			userService.update(user);
		}
		CommandGroup cg = new CommandGroup();
		cg.add(new JSCommand("$.dialog(this).ownerView.cmd('search');$.close(this);"));
		cg.add(new AlertCommand("保存成功").setPosition(AlertCommand.POSITION_SOUTHEAST).setTimeout(5));
		return cg;
	}

	@RequestMapping("/delete")
	@ResponseBody
	public Object delete(HttpServletRequest request) throws Exception {
		String userId = request.getParameter("userId");
		if (Utils.isEmpty(userId)) {
			throw new DfishException("所选记录为空");
		}
		String[] userIds = userId.split(",");
		// FIXME 这里是简单做法,性能高的做法需要做批量方法
		for (String id : userIds) {
			userService.delete(id);
		}
		CommandGroup cg = new CommandGroup();
		cg.add(new JSCommand("VM(this).cmd('search')"));
		cg.add(new AlertCommand("删除成功").setPosition(AlertCommand.POSITION_SOUTHEAST).setTimeout(5));
		return cg;
	}

}
